import { check, checkError, getRequest } from "./baseUtils"

class UserApi {
	static baseUrl = "/user"
	
	/**
	 * 登录
	 * user { phone: string, code: string }
	 */
	static async login(user) {
		uni.showLoading({title: "登录中", mask: true})
		try {
			const response = await uni.request(getRequest({url: `${this.baseUrl}/login`, method: "POST", data: user}))
			return check(response)
		}
		catch(error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
	static async rename(name) {
		uni.showLoading({title: '更新中...', mask: true})
		try {
			const response = await uni.request(getRequest({url: `${this.baseUrl}/rename?name=${name}`, method: "PUT"}))
			return check(response)
		} 
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
	static async avatar(imagePath) {
		uni.showLoading({title: '更新中...', mask: true})
		try {
			const response = await uni.uploadFile(getRequest({url: `${this.baseUrl}/avatar`, filePath: imagePath, name: 'file'}))
			return check(response)
		} 
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
	static async recent() {
		uni.showLoading({title: '更新中...', mask: true})
		try {
			const response = await uni.request(getRequest({
				url: `${this.baseUrl}/recent`
			}))
			return check(response)
		} 
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
}

const key = "userinfo"
const recent = "user_recent"

/**
 * 数据结构
 *   Userinfo
 *     username: string
 * 	   token: string
 * 	   phone: string
 * 	   image: string
 */
export class UserUtils {
	
	static recent(success) {
		const userRecent = uni.getStorageSync(recent)
		if (userRecent) {
			success(userRecent)
		}
		else {
			UserApi.recent()
			.then(data => {
				uni.setStorageSync(recent, data)
				success(data)
			})
		}
	}
	
	static reloadRecent(success) {
		UserApi.recent()
		.then(data => {
			uni.setStorageSync(recent, data)
			success(data)
		})
	}
	
	/**
	 * 登录
	 * user { phone: string, code: string }
	 */
	static login(user) {
		UserApi.login(user)
		.then(data => {
			// 将数据存入缓存
			uni.setStorageSync(key, data)
			// 跳转到主页
			uni.switchTab({url: '/pages/category/category'})
		})
	}
	
	static getUser() {
		const userinfo = uni.getStorageSync(key)
		if (userinfo) {
			// 返回
			return {
				username: userinfo.username,
				phone: userinfo.phone,
				image: userinfo.image
			}
		}
		else {
			// 没拿到提醒登录过期，返回登录页面
			uni.showToast({title: "登录过期，请重新登录", icon: "none"})
			setTimeout(() => {uni.redirectTo({url: '/pages/login/login'})}, 1500)
		}
	}
	
	static rename(name, success) {
		UserApi.rename(name)
		.then(data => {
			let userinfo = uni.getStorageSync(key)
			userinfo.username = name
			uni.setStorageSync(key, userinfo)
			success(name)
		})
	}
	
	static avatar(imagePath, success) {
		UserApi.avatar(imagePath)
		.then(data => {
			let userinfo = uni.getStorageSync(key)
			userinfo.image = data
			uni.setStorageSync(key, userinfo)
			success(data)
		})
	}
	
	static clear() {
		uni.removeStorageSync(key)
	}
	
	static getToken() {
		return uni.getStorageSync(key).token
	}
	
}